package com.example.demo.cmd;

import org.apache.commons.io.IOUtils;
import org.junit.Test;
import sun.security.x509.X509CertImpl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;


import java.security.*;
import java.security.cert.CertificateException;
import java.util.Enumeration;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;


import java.security.cert.Certificate;


public class Test2 {

    @Test
    public void test() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException {
        String certP12Url = "https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/2022/09/15/67d85ed8-0662-4e5d-a5d4-7da6058ab30e.p12";
        String merChantNumber = "1613220424";
        KeyStore keyStore = KeyStore.getInstance("PKCS12");

        //密码
        char[] nPassword = null;
        if ((merChantNumber == null) || merChantNumber.trim().equals("")) {
            nPassword = null;
        } else {
            nPassword = merChantNumber.toCharArray();
        }

        //读取p12文件
        InputStream inputStream = getInputStreamByUrl(certP12Url);

        keyStore.load(inputStream, merChantNumber.toCharArray());
        inputStream.close();

        System.out.println("keystore type=" + keyStore.getType());

        Enumeration<String> aliases = keyStore.aliases();
        String keyAlias = null;

        if (aliases.hasMoreElements()) {
            keyAlias = (String) aliases.nextElement();
            System.out.println("alias=[" + keyAlias + "]");
        }

        System.out.println("is key entry=" + keyStore.isKeyEntry(keyAlias));
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, nPassword);
        System.out.println("private key = " + privateKey);

        Certificate cert = keyStore.getCertificate(keyAlias);
        PublicKey pubkey = cert.getPublicKey();

        X509CertImpl cert1 = (X509CertImpl) cert;

        String s = cert1.getSerialNumber().toString(16);
        System.out.println("serialNumber = " + s);
        System.out.println("cert class = " + cert.getClass().getName());
        System.out.println("cert = " + cert);
        System.out.println("public key = " + pubkey);

    }


    /**
     * 根据地址获得数据的输入流
     *
     * @param strUrl 网络连接地址
     * @return url的输入流
     */
    public InputStream getInputStreamByUrl(String strUrl) {
        HttpURLConnection conn = null;
        try {
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(20 * 1000);
            final ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(conn.getInputStream(), output);
            return new ByteArrayInputStream(output.toByteArray());
        } catch (Exception e) {

        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                }
            } catch (Exception e) {

            }
        }
        return null;
    }


}
